package com.mars.controller;

import com.mars.common.JsonData;
import com.mars.exception.PermissionException;
import com.mars.param.TestVo;
import com.mars.util.BeanValidator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * @author MARS
 * @date 2017/12/21
 */

@Controller
@RequestMapping("/test")
@Slf4j
public class TestController {

    @RequestMapping("/hello.json")
    @ResponseBody
    public JsonData hello(){
        log.info("hello");
        throw new PermissionException("test permissionException");
//        return JsonData.success("hello,permission");
    }

    @RequestMapping("/validate.json")
    @ResponseBody
    public JsonData validate(TestVo testVo){
        log.info("validate");
        try{
            Map<String ,String> map = BeanValidator.validateObject(testVo);
            if (MapUtils.isNotEmpty(map)){
//                遍历
                for(Map.Entry<String,String> entry: map.entrySet()){
                    log.info("{}->{}",entry.getKey(),entry.getValue());
                }
            }
        } catch (Exception e){

        }

        return JsonData.success("test validate");
    }
}
